#ifndef ARMOR_DETECTOR_H
#define ARMOR_DETECTOR_H

#include <opencv2/opencv.hpp>
#include <vector>
#include "armor.h"  // 包含统一的 Armor 类

struct LightBar {
    cv::RotatedRect rect;
    float length;
    float width;
    cv::Point2f center;
    std::vector<cv::Point2f> endpoints;
};

class ArmorDetector {
public:
    ArmorDetector();
    bool loadParams(const std::string& config_path);
    void setEnemyColor(int color); // 0: blue, 1: red
    void setBinaryThresh(int t) { binary_thresh_ = t; }
    std::vector<Armor> detect(const cv::Mat& frame);
    void drawResults(cv::Mat& frame, const std::vector<Armor>& armors);
    // Try reload params if config file changed
    void tryReloadParams();
    
private:
    void preprocess(const cv::Mat& src, cv::Mat& binary);
    std::vector<LightBar> findLightBars(const cv::Mat& binary);
    std::vector<Armor> matchLightBars(const std::vector<LightBar>& light_bars);
    
    int enemy_color_;
    cv::Mat kernel_;
    
    // 参数
    int binary_thresh_;
    double min_lightbar_area_;
    double max_lightbar_ratio_;
    double min_armor_ratio_;
    double max_armor_ratio_;
    // config hot-reload
    std::string config_path_;
    std::time_t last_mtime_ = 0;
};

#endif